import Flutter
import UIKit
import HCBio

public class HcBioSdkPlugin: NSObject, FlutterPlugin {
  private static var channel: FlutterMethodChannel?
    
  public static func register(with registrar: FlutterPluginRegistrar) {
    channel = FlutterMethodChannel(name: "hc_bio_sdk", binaryMessenger: registrar.messenger())
    let instance = HcBioSdkPlugin()
    registrar.addMethodCallDelegate(instance, channel: channel!)
    instance.listenNotification()
  }

  public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
      guard let methodHandler = MethodHandler(rawValue: call.method) else {
          result(FlutterMethodNotImplemented)
          return
      }
      methodHandler.handle(call,result:result)
  }
}


extension HcBioSdkPlugin {
    func listenNotification() {
        // Do any additional setup after loading the view, typically from a nib.
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(onNote(_ :)),
                                               name: NSNotification.Name(HCBioNotificaitonType.HRV.rawValue),
                                               object: nil)
        
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(onNote(_ :)),
                                               name: NSNotification.Name(HCBioNotificaitonType.MindRead.rawValue),
                                               object: nil)
        
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(onNote(_ :)),
                                               name: NSNotification.Name(HCBioNotificaitonType.Wave.rawValue),
                                               object: nil)
        
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(onNote(_ :)),
                                               name: NSNotification.Name(HCBioNotificaitonType.MainIndicator.rawValue),
                                               object: nil)
        
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(onNote(_ :)),
                                               name: NSNotification.Name(HCBioNotificaitonType.HeartRate.rawValue),
                                               object: nil)
    }
    
    @objc func onNote(_ note: Notification) {
        guard let noteType = HCBioNotificaitonType(rawValue: note.name.rawValue),
              let datsStr = note.object as? String
        else {
            return
        }
        
        var dataType = ""
        switch noteType {
        case .HRV:
            dataType = "HRV"
        case .HeartRate:
            dataType = "HeartRate"
        case .MainIndicator:
            dataType = "MainIndicator"
        case .MindRead:
            dataType = "MindRead"
        case .Wave:
            dataType = "Wave"
        default:
            debugPrint("unknown \(#function)")
        }
        
        let data = [
            "type": dataType,
            "value": datsStr
        ]
        HcBioSdkPlugin.channel?.invokeMethod("onReceiveBioData", arguments: data)
    }
}
